﻿using System;
using System.Collections.Generic;
using BLL.IServices;


namespace BLL
{
    public class WebProvider
    {
        #region Contructor and Utils

        private readonly Dictionary<Type, object> _implementCenter = new Dictionary<Type, object>();

        private readonly object _lock = new object();


        private T GetImplementObject<T>() where T : class
        {
            lock (_lock)
            {
                var typeKey = typeof (T);
                if (_implementCenter.ContainsKey(typeKey))
                    return (T) _implementCenter[typeKey];
                var instance = Activator.CreateInstance<T>();
                _implementCenter.Add(typeKey, instance);
                return instance;
            }
        }

        private WebProvider()
        {
        }

        private static WebProvider _webProvider;

        public static WebProvider Provider
        {
            get { return _webProvider ?? (_webProvider = new WebProvider()); }
        }

        #endregion

        #region Object Provider

        private UserService _user;
        public UserService User
        {
            get { return _user ?? (_user = GetImplementObject<UserService>()); }
        }

        #endregion
    }
}
